﻿namespace Budgetsch.Web.Services
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceModel.Activation;
    using System.Web;

    using Budgetsch.Web.EntityModel;

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class WP7DataService : IWP7DataService
    {
        private readonly BudgetschEntities _ctx;

        public WP7DataService()
        {
            _ctx = new BudgetschEntities();
        }

        #region Implementation of IWP7DataService

        public IEnumerable<Income> GetIncomes()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                var userName = HttpContext.Current.User.Identity.Name;
                return _ctx.Incomes.Where(inc => inc.User.UserName.Equals(userName)).ToList();
            }
            else
                return new List<Income>();

        }

        public IEnumerable<Cost> GetCosts()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                var userName = HttpContext.Current.User.Identity.Name;
                return _ctx.Costs.Where(cost => cost.User.UserName.Equals(userName)).ToList();
            }
            else
                return new List<Cost>();
        }

        public IEnumerable<Saving> GetSavings()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                var userName = HttpContext.Current.User.Identity.Name;
                return _ctx.Savings.Where(sav => sav.User.UserName.Equals(userName)).ToList();
            }
            else
                return new List<Saving>();
        }

        public void AddIncome(Income income)
        {
            Debug.WriteLine(HttpContext.Current.User.Identity.Name);
            _ctx.AddToIncomes(income);
            _ctx.SaveChanges();
        }

        public void AddCost(Cost cost)
        {
            Debug.WriteLine(HttpContext.Current.User.Identity.Name);
            _ctx.AddToCosts(cost);
            _ctx.SaveChanges();
        }

        public void AddSaving(Saving saving)
        {
            Debug.WriteLine(HttpContext.Current.User.Identity.Name);
            _ctx.AddToSavings(saving);
            _ctx.SaveChanges();
        }

        public bool SetPublicity(bool publicity)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                var userName = HttpContext.Current.User.Identity.Name;
                var q = this._ctx.Users.Where(user => user.UserName.Equals(userName)).First();
                q.IsSharing = publicity;
                try
                {
                    _ctx.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            return false;
        }

        public IncomeComment AddIncomeComment(string message, int itemID)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                var userName = HttpContext.Current.User.Identity.Name;
                var userId = _ctx.Users.Where(u => u.UserName == userName).FirstOrDefault().ID;
                var comment = new IncomeComment { Comment = message, Time = DateTime.Now, UserID = userId };
                _ctx.Incomes.Where(inc => inc.ID == itemID).FirstOrDefault().IncomeComments.Add(comment);

                try
                {
                    _ctx.SaveChanges();
                    return comment;
                }
                catch (Exception)
                {
                    return null;
                }
            }
            return null;
        }

        public CostComment AddCostComment(string message, int itemID)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                var userName = HttpContext.Current.User.Identity.Name;
                var userId = _ctx.Users.Where(u => u.UserName == userName).FirstOrDefault().ID;
                var comment = new CostComment { Comment = message, Time = DateTime.Now, UserID = userId };
                _ctx.Costs.Where(inc => inc.ID == itemID).FirstOrDefault().CostComments.Add(comment);

                try
                {
                    _ctx.SaveChanges();
                    return comment;
                }
                catch (Exception)
                {
                    return null;
                }
            }
            return null;
        }

        public SavingComment AddSavingComment(string message, int itemID)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                var userName = HttpContext.Current.User.Identity.Name;
                var userId = _ctx.Users.Where(u => u.UserName == userName).FirstOrDefault().ID;
                var comment = new SavingComment { Comment = message, Time = DateTime.Now, UserID = userId };
                _ctx.Savings.Where(inc => inc.ID == itemID).FirstOrDefault().SavingComments.Add(comment);

                try
                {
                    _ctx.SaveChanges();
                    return comment;
                }
                catch (Exception)
                {
                    return null;
                }
            }
            return null;
        }

        public IEnumerable<User> GetPublicUsers()
        {
            return this._ctx.Users.Where(user => user.IsSharing.Equals(true)).ToList();
        }


        public IEnumerable<Income> GetUserPublicIncomes(string userName)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return _ctx.Incomes.Where(inc => inc.User.UserName.Equals(userName)).ToList();
            }
            return new List<Income>();
        }

        public IEnumerable<Cost> GetUserPublicCosts(string userName)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return _ctx.Costs.Where(cost => cost.User.UserName.Equals(userName)).ToList();
            }
            return new List<Cost>();
        }

        public IEnumerable<Saving> GetUserPublicSavings(string userName)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return _ctx.Savings.Where(sav => sav.User.UserName.Equals(userName)).ToList();
            }
            return new List<Saving>();
        }

        public bool IsSharedUser()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return _ctx.Users.Where(user => user.UserName.Equals(HttpContext.Current.User.Identity.Name)).Select(user => user.IsSharing).FirstOrDefault();
            }
            return false;
        }

        #endregion
    }
}
